package com.emip.servlet.baseinfo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emip.bean.AlarmConfigBean;
import com.emip.bean.DeviceBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.AlarmConfigService;
import com.emip.service.DeviceService;
import com.emip.service.MonitorTypeService;
import com.emip.util.TableFormat;


@Controller
@RequestMapping(value="/base/device")
public class DeviceAction {

	@Autowired
	MonitorTypeService monitorTypeService;
	
	@Autowired
	AlarmConfigService alarmConfigService;
	
	@Autowired
	DeviceService deviceService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/baseInfo/DeviceManagement";
	}
	
	@RequestMapping(value="/getAllDevices")
	public @ResponseBody Table<DeviceBean> getAllDevices() {
		List<DeviceBean> result = deviceService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<DeviceBean>().formTable(totalPages, 1, records, result);
	}
	
	@RequestMapping(value="/getDeviceByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody DeviceBean getDeviceByID(@RequestParam("eid") int id) {
		return deviceService.getDeviceByID(id);
	}
	
	@RequestMapping(value="/addDevice",method=RequestMethod.POST)
	public @ResponseBody DeviceBean addDevice(@RequestParam("mTypeID") int mTypeID, @RequestParam("deviceType") String deviceType,
			@RequestParam("devicename") String devicename, @RequestParam("deviceDisplayName") String deviceDisplayName,
			@RequestParam("instructionCode") String instructionCode, @RequestParam("deviceImageUrl") String deviceImageUrl, @RequestParam("dWordSeq") int dWordSeq,
			@RequestParam("bitSeq") int bitSeq, @RequestParam("statusValue") double statusValue, @RequestParam("statDesc") String statDesc, @RequestParam("controllFlag") int controllFlag,
			@RequestParam("statImageUrl") String statImageUrl, @RequestParam("alarmConfigID") int alarmConfigID,
			@RequestParam("alarmContent") String alarmContent, @RequestParam("remark") String remark) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(mTypeID);
		AlarmConfigBean aBean = alarmConfigService.getAlarmConfigByID(alarmConfigID);
	
		return deviceService.addDevice(mBean, deviceType, devicename, deviceDisplayName, 
				instructionCode, deviceImageUrl, dWordSeq, bitSeq, statusValue, statDesc, 
				controllFlag, statImageUrl, aBean, alarmContent, remark); 
	}
	
	@RequestMapping(value="/updateDevice",method=RequestMethod.POST)
	public @ResponseBody DeviceBean updateDevice(@RequestParam("id") int id, @RequestParam("mTypeID") int mTypeID, @RequestParam("deviceType") String deviceType,
			@RequestParam("devicename") String devicename, @RequestParam("deviceDisplayName") String deviceDisplayName,
			@RequestParam("instructionCode") String instructionCode, @RequestParam("deviceImageUrl") String deviceImageUrl, @RequestParam("dWordSeq") int dWordSeq,
			@RequestParam("bitSeq") int bitSeq, @RequestParam("statusValue") double statusValue, @RequestParam("statDesc") String statDesc, @RequestParam("controllFlag") int controllFlag,
			@RequestParam("statImageUrl") String statImageUrl, @RequestParam("alarmConfigID") int alarmConfigID,
			@RequestParam("alarmContent") String alarmContent, @RequestParam("remark") String remark) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(mTypeID);
		AlarmConfigBean aBean = alarmConfigService.getAlarmConfigByID(alarmConfigID);
		
		return deviceService.updateDevice(id, mBean, deviceType, devicename, deviceDisplayName, 
				instructionCode, deviceImageUrl, dWordSeq, bitSeq, statusValue, statDesc, 
				controllFlag, statImageUrl, aBean, alarmContent, remark);		
	}
	
	@RequestMapping(value="/deleteDevice",method=RequestMethod.POST)
	public @ResponseBody String deleteDevice(@RequestParam("eID") int id) {
		deviceService.deleteDevice(id);
		return "success";
	}
	
	@RequestMapping(value="/getAllDevicesObj",method=RequestMethod.POST)
	public @ResponseBody List<DeviceBean> getAllDevicesObj() {
		return deviceService.getAll();
	}
}
